package com.gx.util.commonutil;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;
import java.util.stream.Stream;

/**
 * Created by gx on 2017/7/3.
 */
@Slf4j
public class DateUtil {
    private static final FastDateFormat DATE_FORMAT;
    public static final TimeZone CHINA_TIMEZONE = TimeZone.getTimeZone("GMT+8");
    private static final Map<String, FastDateFormat> defaultFormats;
    private static final FastDateFormat TIME_FORMAT;
    private static final FastDateFormat STANDARD_FORMAT;

    static {
        TIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss", CHINA_TIMEZONE);
        STANDARD_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZ", CHINA_TIMEZONE);

        DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd", CHINA_TIMEZONE);
        defaultFormats = ImmutableMap.<String, FastDateFormat>builder().put("yyyy-MM-dd",DATE_FORMAT).put("yyyy-MM-dd HH:mm:ss",TIME_FORMAT).put("yyyy-MM-dd'T'HH:mm:ss.SSSZ",STANDARD_FORMAT).put("yyyyMMdd",FastDateFormat.getInstance("yyyyMMdd", CHINA_TIMEZONE)).put("yyyyMMddHHmmss",FastDateFormat.getInstance("yyyyMMddHHmmss", CHINA_TIMEZONE)).put("yyyyMMddHHmmssSSS",FastDateFormat.getInstance("yyyyMMddHHmmssSSS", CHINA_TIMEZONE)).put("yyyy-M-dd",FastDateFormat.getInstance("yyyy-M-dd", CHINA_TIMEZONE)).put("yyyy-MM",FastDateFormat.getInstance("yyyy-MM", CHINA_TIMEZONE)).put("yyyy-M", FastDateFormat.getInstance("yyyy-M", CHINA_TIMEZONE)).put("yyyyMddHHmmss", FastDateFormat.getInstance("yyyyMddHHmmss", CHINA_TIMEZONE)).put("yyyyMM", FastDateFormat.getInstance("yyyyMM", CHINA_TIMEZONE)).put("yyyyM", FastDateFormat.getInstance("yyyyM", CHINA_TIMEZONE)).put("yyyy/MM/dd", FastDateFormat.getInstance("yyyy/MM/dd", CHINA_TIMEZONE)).put("yyyy/MM/dd HH:mm:ss", FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss", CHINA_TIMEZONE)).put("yyyy/MM/dd'T'HH:mm:ss.SSSZ", FastDateFormat.getInstance("yyyy/MM/dd'T'HH:mm:ss.SSSZ", CHINA_TIMEZONE)).put("yyyy/M/dd", FastDateFormat.getInstance("yyyy/M/dd", CHINA_TIMEZONE)).put("yyyy/MM", FastDateFormat.getInstance("yyyy/MM", CHINA_TIMEZONE)).put("yyyy/M", FastDateFormat.getInstance("yyyy/M", CHINA_TIMEZONE)).build();

    }
    public void getTimestampByjava8(){
        LocalDateTime.now().toString();
    }

    public static String toDateFormat(Date date) {
        Preconditions.checkArgument(date != null, "时间对象不能为空");
        return DATE_FORMAT.format(date);
    }
    public static Date parseDate(String dateStr, String... patterns) {
        Preconditions.checkArgument(StringUtils.isNotBlank(dateStr), "无法将空字符串转换为Date对象");
        Preconditions.checkArgument(patterns.length > 0, "parseDate时必须指定至少一种格式");
        Date result = (Date) Stream.of(patterns).map((pattern) -> {
            FastDateFormat format;
            if (defaultFormats.containsKey(pattern)) {
                format = (FastDateFormat)defaultFormats.get(pattern);
            } else {
                format = FastDateFormat.getInstance(pattern, CHINA_TIMEZONE);
            }

            try {
                return format.parse(dateStr);
            } catch (Exception var4) {
                return null;
            }
        }).filter((date) -> {
            return date != null;
        }).findFirst().orElse(null);
        if (result == null) {
            log.warn("无法将{}换格式转换为格式{}的时间对象", dateStr, Arrays.asList(patterns));
        }

        return result;
    }

    public static int compareYear(Date date1, Date date2) {
        Preconditions.checkArgument(date1 != null && date2 != null, "时间对象不能为空");
        int year1 = DateUtils.toCalendar(date1).get(1);
        int year2 = DateUtils.toCalendar(date2).get(1);
        return year1 - year2;
    }

    public static String toDateFormat(String s) {
        return StringUtils.isBlank(s) ? null : toDateFormat(parseDate(s));
    }


}
